/*
 * Board.fx
 * Created on 04/03/2009
 */

package changecolor.model;

import changecolor.model.CarPart;
import javafx.fxd.FXDLoader;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.shape.Shape;

/**
 * @author José Corcuera
 */

public class Board extends CustomNode{
    var _root: Node;
    public var url: String;
    var capota: CarPart;
    var chasis: CarPart;
    var parachoque: CarPart;
    var background;
    public var optionpanel:OptionPanel;
    public var width:Number;
    public var height:Number;
    def hue: Number = bind optionpanel.hue on replace{
        if(optionpanel.rbAll.selected){
            capota.hue = hue;
            chasis.hue = hue;
            parachoque.hue = hue;
        }else if(optionpanel.rbCapota.selected){
            capota.hue = hue;
        }else if(optionpanel.rbChasis.selected){
            chasis.hue = hue;
        }else if(optionpanel.rbParachoques.selected){
            parachoque.hue = hue;
        }
    }

    def sat:Number = bind optionpanel.saturation on replace{
        if(optionpanel.rbAll.selected){
            chasis.saturation = sat;
            capota.saturation = sat;
            parachoque.saturation = sat;
        }else if(optionpanel.rbCapota.selected){
            capota.saturation = sat;
        }else if(optionpanel.rbChasis.selected){
            chasis.saturation = sat;
        }else if(optionpanel.rbParachoques.selected){
            parachoque.saturation = sat;
        }
    }

    def bri:Number = bind optionpanel.brightness on replace{
        if(optionpanel.rbAll.selected){
            parachoque.brightness = bri;
            chasis.brightness = bri;
            capota.brightness = bri;
        }else if(optionpanel.rbCapota.selected){
            capota.brightness = bri;
        }else if(optionpanel.rbChasis.selected){
            chasis.brightness = bri;
        }else if(optionpanel.rbParachoques.selected){
            parachoque.brightness = bri;
        }
    }

    init{
        var content = FXDLoader.loadContent(url);
        background = content.getNode("background");
        if(background != null){
            background.cache = true;
            width = background.boundsInLocal.maxX - background.boundsInLocal.minX;
            height = background.boundsInLocal.maxY - background.boundsInLocal.minY;
        }

        var shapeplus: Shape[];
        var shapeless: Shape[];
        var shapetemp: Shape;
        ///Capota
        shapeplus = content.getShape("capotaplus");
        shapeless = content.getShape("capotaless");
        capota = CarPart {
            shapeplus: shapeplus
            shapeless: shapeless
        };
        //Chasis
        shapeplus = content.getShape("chasisplus");
        shapeless = content.getShape("chasisless");
        chasis = CarPart {
            shapeplus: shapeplus
            shapeless: shapeless
        };
        //Parachoque
        shapeplus = content.getShape("parachoqueplus");
        for(i in [1..4]){
            shapetemp = content.getShape("parachoqueless_{i}");
        insert shapetemp into shapeless;
        }
        parachoque = CarPart {
            shapeplus: shapeplus
            shapeless: shapeless
        };


    }

    override function create():Node{
        return
        _root = Group{
            content: [
                background
                capota.node
                chasis.node
                parachoque.node
            ]
        }

    }


}